<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="__STATIC__/template/lib/html5shiv.js"></script>
    <script type="text/javascript" src="__STATIC__/template/lib/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="__STATIC__/template/lib/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="__STATIC__/template/static/h-ui.admin/css/style.css" />
    <!--[if IE 6]>
    <script type="text/javascript" src="__STATIC__/template/lib/DD_belatedPNG_0.0.8a-min.js" ></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <style>
        /*分页样式*/
        .pagination{text-align:center;margin-top:20px;margin-bottom: 20px;}
        .pagination li{margin:0px 10px; border:1px solid #e6e6e6;padding: 3px 8px;display: inline-block;}
        .pagination .active{background-color: #46A3FF;color: #fff;}
        .pagination .disabled{color:#aaa;}
        a:hover{color: #ff081f; /*鼠标在a上面时,变蓝色*/}
        #DataTables_Table_0_info {
            display: none;
        }
        #DataTables_Table_0_paginate{
            display: none;
        }
    </style>
    <title>权限菜单</title>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页
    <span class="c-gray en">&gt;</span> 管理员管理
    <span class="c-gray en">&gt;</span> <span style="color: red">权限菜单</span>
    <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" >
        <i class="Hui-iconfont">&#xe68f;</i>
    </a>
</nav>

<div class="page-container">
    <div class="text-c">
        {empty name='change'}
        <span class="select-box" style="width:120px">
              <select class="select" size="1" name="demo1" id="change" onclick="change()">
                  <option value="1" selected>栏目名</option>
                  <option value="2">菜单名</option>
                  <option value="3">地址</option>
              </select>
        </span>
        {else /}
        <span class="select-box" style="width:120px">
              <select class="select" size="1" name="demo1" id="change" onclick="change()">
                  <option value="1" {$change == 1 ? "selected='selected'" : ''}>栏目名</option>
                  <option value="2" {$change == 2 ? "selected='selected'" : ''}>菜单名</option>
                  <option value="3" {$change == 3 ? "selected='selected'" : ''}>地址</option>
              </select>
        </span>
        {/empty}
        {empty name="item_id"}
            <span class="select-box" style="width:120px" id="item">
                  <select class="select" size="1" name="demo1" id="id">
                      {volist name="items" id="v"}
                      <option value="{$v.id}">{$v.item_name}</option>
                      {/volist}
                  </select>
            </span>
        {else /}
            <span class="select-box" style="width:120px" id="item">
                  <select class="select" size="1" name="demo1" id="id">
                      <option value="{$item_id}">{$item_name?$item_name:''}</option>
                      {volist name="items" id="v"}
                      <option value="{$v.id}">{$v.item_name}</option>
                      {/volist}
                  </select>
            </span>
        {/empty}
        <input type="text" value="{$name?$name:''}" class="input-text" style="width:250px;display: none" placeholder="输入菜单名称" id="menu_name" name="menu_name">
        <input type="text" value="{$url?$url:''}" class="input-text" style="width:250px;display: none" placeholder="输入控制器/方法" id="menu_url" name="menu_url">
        <button type="submit" class="btn btn-success" id="search" name=""><i class="Hui-iconfont">&#xe665;</i> 搜菜单</button>
    </div>
    <div class="cl pd-5 bg-1 bk-gray mt-20">
         <span class="r">共有数据：<strong>{$count}</strong> 条</span>
    </div>

    <div class="mt-20">
        <table class="table table-border table-bordered table-hover table-bg">
        <thead>
        <tr class="text-c">
            <th width="40">ID</th>
            <th width="200">栏目名</th>
            <th width="200">菜单名</th>
            <th>URL</th>
            <th width="300">是否启用</th>
            <th width="100">操作</th>
        </tr>
        </thead>
        <tbody>
        {volist name="menu" id="v" empty='$empty'}
            <tr class="text-c">
                <td>{$v.id}</td>
                <td>{$v.item_name}</td>
                <td>{$v.name}</td>
                <td>{$v.url}</td>
                <td>
                    {if condition="$v.state eq 1"}
                    <span class="label label-success radius">已启用</span>
                    {else /}
                    <span class="label label-defaunt radius">已停用</span>
                    {/if}
                </td>
                <td class="f-14">
                    {if condition="$v.state eq 1"}
                    <a style="text-decoration:none" onClick="menu_stop(this,'{$v.id}')" href="javascript:;" title="下架">
                        <i class="Hui-iconfont">&#xe6de;</i>
                    </a>
                    {else /}
                    <a style="text-decoration:none" onClick="menu_start(this,'{$v.id}')" href="javascript:;" title="发布">
                        <i class="Hui-iconfont">&#xe603;</i>
                    </a>
                    {/if}
                    <a title="编辑" href="javascript:;" data-item-id="{$v.item_id}" data-id="{$v.id}" onclick="menu_edit(this,'{$v.id}')" style="text-decoration:none">
                        <i class="Hui-iconfont">&#xe6df;</i>
                    </a>
                    <a title="删除" href="javascript:;" onclick="menu_del(this,'{$v.id}')" class="ml-5" style="text-decoration:none">
                        <i class="Hui-iconfont">&#xe6e2;</i>
                    </a>
                </td>
            </tr>
        {/volist}
        </tbody>
    </table>
        {$menu->render()}
   </div>
    <div class="page-container" id="form-menu-add" >
        <form action="" method="post" class="form form-horizontal" enctype="multipart/form-data" onsubmit="return checkAdd()">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>菜单功能：</label>
                <input type="text" class="input-text"  value='' placeholder="权限名称"  id="name" name="name" style="width: 500px">
                <span id="name_tip"></span>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>项目：</label>
                <span class="select-box" style="width:500px;">
                  <select class="select" size="1" name="item_id" id="item_id">
                      {volist name="items" id="v"}
                      <option value="{$v.id}">{$v.item_name}</option>
                      {/volist}
                  </select>
                </span>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2">权限控制url：</label>
                <input type="text" class="input-text" width="500" value='' placeholder="url地址为控制器/方法" id="url" name="url" style="width: 500px">
                <span id="url_tip"></span>
            </div>
            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                    <button id="menuAdd" class="btn btn-primary radius" >
                        <i class="Hui-iconfont">&#xe632;</i> 保存并添加
                    </button>
                </div>
            </div>
        </form>

    </div>
    <!--菜单 - 修改-->
    <div class="page-container" style="display:none" id="menu_edit">
        <form action="" method="post" class="form form-horizontal" id="" enctype="multipart/form-data" onsubmit="return checkEdit()">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>菜单功能：</label>
                <input type="text" class="input-text"  value='' placeholder="权限名称"  id="name_x" name="name_x" style="width: 500px">
                <span id="nameX_tip"></span>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>项目：</label>
                <span class="select-box" style="width:500px;">
                  <select class="select" size="1" name="itemId" id="itemId">
                      <option value="" id="default"></option>
                      {volist name="items" id="v"}
                      <option value="{$v.id}">{$v.item_name}</option>
                      {/volist}
                  </select>
                </span>
            </div>

            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2">权限控制url：</label>
                <input type="text" class="input-text" width="500" value='' placeholder="url地址为控制器/方法" id="url_x" name="url_x" style="width: 500px">
                <span id="urlX_tip"></span>
            </div>
            <input type="hidden" class="input-text"  value='' name="menu_id" id="menu_id"/>
            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                    <button id="menuEdit" class="btn btn-primary radius" >
                        <i class="Hui-iconfont">&#xe632;</i> 保存并修改
                    </button>
                </div>
            </div>
        </form>

    </div>
</div>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="__STATIC__/template/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="__STATIC__/template/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="__STATIC__/template/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="__STATIC__/template/static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__STATIC__/template/lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
      $('#menuAdd').click(menuAdd);
      $('#menuEdit').click(menuEdit);
      $('#search').click(search);

      //验证
      $('#name').blur(checkName);
      $('#url').blur(checkUrl);

      $('#name_x').blur(checkNameX);
      $('#url_x').blur(checkUrlX);
      $('#itemId').change(checkItemIdX)
  });
  /*异步验证 - 菜单名*/
  var name_tip = false;
  function checkName(){
      var name = $('#name').val();
      var url  = "{:url('admin/menu/checkName')}";
      $.post(url,{ name:name },function(res){
          //console.log(res);
          if(res==1){
              $('#name_tip').text('菜单名不能重复').css({'color':'red','font-size':'12px'});
              return name_tip = false;
          }else{
              $('#name_tip').text('√').css('color','green');
              return name_tip = true;
          }

      })
  }
  var url_tip = false;
  function checkUrl(){
      var url = $('#url').val();
      var rel = /^\w{1,}\/\w{1,}$/;
      if(rel.test(url)=== false){
          $('#url_tip').text('格式不正确').css({'color':'red','font-size':'12px'});
          return url_tip = false ;
      }
      $.post("{:url('admin/menu/checkUrl')}",{ url:url },function(res){
          console.log(res);
          if(res==1){
              $('#url_tip').text('地址名不能重复').css({'color':'red','font-size':'12px'});
             return url_tip = false;
          }else{
              $('#url_tip').text('√').css('color','green');
              return url_tip = true;
          }

      })

  }
  function checkAdd(){
      if(name_tip==true&&url_tip==true){
          return true;
      }
      return false;
  }
  /*菜单搜索选择*/
  function change(){
      var change = $("#change").val();
      if(change==2){
          $('#menu_name').show();
          $('#menu_url').hide();
          $('#item').hide();
      }
      if(change==3){
          $('#menu_url').show();
          $('#menu_name').hide();
          $('#item').hide();
      }
      if(change==1){
          $('#item').show();
          $('#menu_url').hide();
          $('#menu_name').hide();

      }
  }
  /*权限菜单 - 搜索*/
  function search(){
      var change    = $("#change").val();
      var item_id   = $('#id').val();
      var url       = $('#menu_url').val();
      var name      = $('#menu_name').val();
      var item_name = $('#id').text();
      var data    = { change:change,item_id:item_id,url:url,name:name,item_name:item_name };
      $.post("{:url('admin/menu/search')}",data,function(res){
         // console.log(res);
          if(res.ok==false){
              layer.msg(res.msg,{icon:0,time:1000});
          }else{

              $('body').empty();
              $('body').append(res);
              if(change==2){
                  $('#menu_name').show();
                  $('#menu_url').hide();
                  $('#item').hide();
              }
              if(change==3){
                  $('#menu_url').show();
                  $('#menu_name').hide();
                  $('#item').hide();
              }
              if(change==1){
                  $('#item').show();
                  $('#menu_url').hide();
                  $('#menu_name').hide();
                  //  $('#itemShow').text(item_name);

              }
          }

      })
  }
  /*权限菜单 - 添加*/
  function menuAdd(){
      var url     = $('#url').val();
      var name    = $('#name').val();
      var item_id = parseInt($('#item_id').val());
      var data = {
          url:url,
          name:name,
          item_id:item_id
      }
      if(checkAdd()){
          $.post("{:url('admin/Menu/menuAdd')}",data,function(res){
              // console.log(res);
              if(res.ok){
                  layer.msg(res.msg,{icon:1,time:1000});
                  setTimeout("window.location.replace(location.href)",1000);
              }else{
                  layer.msg(res.msg,{icon:5,time:2000});
              }
          })
      }

  }
   /*权限菜单 - 停用*/
  function menu_stop(obj,id){
      layer.confirm('确认要停用吗？',function(index){
          $.post("{:url('admin/Menu/menuStop')}",{ id:id },function(res){
             // console.log(res);
              if(res==1){
                  layer.msg('已停用!',{icon:1,time:1000});
                  setTimeout("window.location.replace(location.href)",1000);
              }else{
                  layer.msg('修改失败!',{icon:1,time:1000});
              }
          })
      })
  }
  /*权限菜单 - 启用*/
  function menu_start(obj,id){
      layer.confirm('确认要启用吗？',function(index){
          $.post("{:url('admin/Menu/menuStart')}",{ id:id },function(res){
              //console.log(res);
              if(res==1){
                  layer.msg('已启用!',{icon:1,time:1000});
                  setTimeout("window.location.replace(location.href)",1000);
              }else{
                  layer.msg('修改失败!',{icon:0,time:1000});
              }
          })
      })
  }

    /*权限菜单-删除*/
    function menu_del(obj,id){
        layer.confirm('菜单删除须谨慎，确认要删除吗？',function(index){
            $.post('{:url(\'admin/Menu/menuDel\')}',{ id:id },function(res){
               // console.log(res);
                if(res==1){
                    layer.msg('已删除!',{icon:1,time:1000});
                    setTimeout("window.location.replace(location.href)",1000);
                }else{
                    layer.msg('删除失败!',{icon:0,time:1000});
                }
            })
        });
    }
  /*异步验证 - 菜单名*/
  var nameX_tip = true;
  function checkNameX(){
      var name    = $('#name_x').val();
      var menu_id = $('#menu_id').val();
      var url  = "{:url('admin/menu/checkNameX')}";
      $.post(url,{ name:name,menu_id:menu_id },function(res){
          //console.log(res);
          if(res==1){
              $('#nameX_tip').text('菜单名不能重复').css({'color':'red','font-size':'12px'});
              return nameX_tip = false;
          }else{
              $('#nameX_tip').text('√').css('color','green');
              return nameX_tip = true;
          }

      })
  }
  /*修 - 异步验证地址*/
  var urlX_tip = true;
  function checkUrlX(){
      var url = $('#url_x').val();
      var menu_id = $('#menu_id').val();
      var rel = /^\w{1,}\/\w{1,}$/;
      if(rel.test(url)=== false){
          $('#urlX_tip').text('格式不正确').css({'color':'red','font-size':'12px'});
          return urlX_tip = false ;
      }
      $.post("{:url('admin/menu/checkUrlX')}",{ url:url,menu_id:menu_id },function(res){
          //console.log(res);
          if(res==1){
              $('#urlX_tip').text('地址名不能重复').css({'color':'red','font-size':'12px'});
              return urlX_tip = false;
          }else{
              $('#urlX_tip').text('√').css('color','green');
              return urlX_tip = true;
          }

      })
  }

    /*权限菜单 - 修改*/
    function menu_edit(obj,id){
        var name = $(obj).parent().prev().prev().prev().text();
        var item_name = $(obj).parent().prev().prev().prev().prev().text();
        var item_id  = $(obj).attr('data-item-id');
        //alert(itemId);
        var url  = $(obj).parent().prev().prev().text();
        $('#menu_edit').show();
        $('#form-menu-add').hide();

        $('#name_x').val(name);
        $('#url_x').val(url);
        $('#menu_id').val(id);

        $('#default').val(item_id);
        $('#default').text(item_name);

    }
  /*验证- 是否改变item_id*/
  function checkItemIdX(){
      var value = $('#default').val();
      var itemId   = $('#itemId').val();
      if(value!=itemId){
          return true;
      }
  }
      function checkEdit(){
          if(urlX_tip==true && nameX_tip==true){
              return true;
          }else if(checkItemIdX()){
              return true;
          }else{
              return false;
          }
          return false;
      }
    /*权限菜单 - 修改保存*/
    function menuEdit(){
        //alert(1);
        var name = $('#name_x').val();
        var url  = $('#url_x').val();
        var id   = $('#menu_id').val();
        var itemId   = $('#itemId').val();

        var data = { name:name,url:url,id:id,itemId:itemId };
        // alert(checkEdit());
        if(checkEdit()){
            $.post("{:url('admin/Menu/menuEdit')}",data,function(res){
                 console.log(res);
                if(res.ok){
                    layer.msg(res.msg,{icon:1,time:1000});
                    setTimeout("window.location.replace(location.href)",1000);
                }else{
                    layer.msg(res.msg,{icon:5,time:1000});
                }
                /*if(res==1){
                    layer.msg('请填写完整的数据!',{icon:0,time:1000});
                }else if(res==2){
                    layer.msg('修改成功!',{icon:1,time:1000});
                    setTimeout("window.location.replace(location.href)",1000);
                }else if(res==3){
                    layer.msg('修改失败!',{icon:5,time:1000});
                }else if(res==4){
                    layer.msg('权限名称或者url地址已经存在！',{icon:0,time:1000})
                }*/
            })
        }

    }
</script>
</body>
</html>